<template>
  <div>
    <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="form.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="form.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="form.manager"
          style="width: 80%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in employees"
            :key="item.id"
            :value="item.username"
            :label="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="form.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
        <el-button size="small" @click="hCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getEmployeeSimple } from '@/api/employees'
import {
  addDepartments,
  getDepartmentsDetail,
  updateDepartmentsDetail
} from '@/api/departments'
export default {
  props: {
    id: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    originList: {
      type: Array,
      required: true
    }
  },
  data() {
    // 验证部门名称是否重复
    const validName = (rule, value, callback) => {
      let existNameist = this.originList.filter(item => item.pid === this.id).map(item => item.name)
      if (this.isEdit) {
        const pid = this.originList.find(item => item.id === this.id).pid
        existNameist = this.originList.filter(item => item.pid === pid).filter(item => item.id !== this.id).map(item => item.name)
      }
      existNameist.includes(value) ? callback(new Error('此部门在同级部门下已存在!')) : callback()
    }
    // 验证部门编码是否重复
    const validCode = (rule, value, callback) => {
      console.log(this.originList.map((item) => item.code))
      const existCodeList = this.originList.map((item) => item.code)
      existCodeList.includes(value)
        ? callback(new Error('部门编码已存在!'))
        : callback()
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        // 部门名称
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '输入的部门名称为1-50个字符',
            trigger: 'blur'
          },
          { validator: validName, trigger: 'blur' }
        ],
        // 部门编码
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '输入的部门编码为1-50个字符',
            trigger: 'blur'
          },
          { validator: validCode, trigger: 'blur' }
        ],
        // 部门管理者
        manager: [
          { required: true, message: '部门负责人必选', trigger: 'blur' }
        ],
        // 部门介绍
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '输入的部门介绍为1-50个字符',
            trigger: 'blur'
          }
        ]
      },
      employees: []
    }
  },
  created() {
    this.loadEmployee()
    if (this.isEdit) {
      this.loadgetDepartmentsDetail()
    }
  },
  methods: {
    // 确定按钮
    hSubmit() {
      // 完成兜底校验
      this.$refs.deptForm.validate((valid) => {
        if (!valid) return
        this.isEdit ? this.doEdit() : this.doAdd()
      })
      // this.doAdd()
      // this.isEdit ? this.doEdit() : this.doAdd()
    },
    hCancel() {
      // 重置表单
      // this.$refs.depDialogRef.resetForm()
      // 取消按钮实现关闭功能
      this.$emit('close')
    },
    // 取部门信息
    async loadgetDepartmentsDetail() {
      try {
        const res = await getDepartmentsDetail(this.id)
        this.form = res.data
      } catch (error) {
        console.log(error)
      }
    },
    async loadEmployee() {
      try {
        const res = await getEmployeeSimple()
        console.log(res, 'getEmployeeSimple')
        this.employees = res.data
      } catch (error) {
        console.log(error)
      }
    },
    // 新增子部门
    // 发送请求
    async doAdd() {
      // 获取到父级的pid 获取方法
      // {pid:this.id,...this.form}
      try {
        const res = await addDepartments({
          pid: this.id,
          ...this.form
        })
        console.log(res, 'addDepartments')
        // this.$emit('父组件事件')
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },
    async doEdit() {
      // 编辑部门
      try {
        const res = await updateDepartmentsDetail({
          id: this.id,
          ...this.form
        })
        console.log(res, 'updateDepartmentsDetail')
        // 关闭对话框
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    }
  }
}

</script>
